#pragma once 
#include "windows.h"

namespace Common
{
	class StringUtility
	{
	public:
		//-------------------------------------------------------------------------------------
		//Description:
		// This function maps a character string to a wide-character (Unicode) string
		//
		//Parameters:
		// lpcszStr: [in] Pointer to the character string to be converted 
		// lpwszStr: [out] Pointer to a buffer that receives the translated string. 
		// dwSize: [in] Size of the buffer
		//
		//Return Values:
		// TRUE: Succeed
		// FALSE: Failed
		// 
		//Example:
		// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
		//---------------------------------------------------------------------------------------
		static bool MByteToWChar(const char* lpcszStr, WCHAR* lpwszStr,DWORD dwSize)
		{
			// Get the required size of the buffer that receives the Unicode 
			// string. 
			DWORD dwMinSize;

			dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);

			if(dwSize < dwMinSize)
			{
				return false;
			}


			// Convert headers from ASCII to Unicode.
			MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);  
			return true;
		}

		//-------------------------------------------------------------------------------------
		//Description:
		// This function maps a wide-character string to a new character string
		//
		//Parameters:
		// lpcwszStr: [in] Pointer to the character string to be converted 
		// lpszStr: [out] Pointer to a buffer that receives the translated string. 
		// dwSize: [in] Size of the buffer
		//
		//Return Values:
		// TRUE: Succeed
		// FALSE: Failed
		// 
		//Example:
		// MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
		//---------------------------------------------------------------------------------------
		static bool WCharToMByte(const WCHAR* lpcwszStr, char* lpszStr,DWORD dwSize)
		{
			DWORD dwMinSize;

			dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
			if(dwSize < dwMinSize)
			{
				return false;
			}
			WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
			return true;
		}

		//don't need to convert
		static bool MByteToTCHAR( const char* lpszStr, char* pszTStr, DWORD dwSize )
		{
			return true;
		}

		static bool MByteToTCHAR( const char* lpszStr, WCHAR* pszTStr, DWORD dwSize )
		{
			return MByteToWChar(lpszStr, pszTStr, dwSize);
		}

		//don't need to convert
		static bool WCHAReToTCHAR( const WCHAR* lpszStr, WCHAR* pszTStr, DWORD dwSize )
		{
			return true;
		}

		static bool WCHAReToTCHAR( const WCHAR* lpszStr, char* pszTStr, DWORD dwSize )
		{
			return WCharToMByte(lpszStr, pszTStr, dwSize);
		}
	};
}